import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    phoneList:[
      { id:1,
        img:"http://img11.360buyimg.com/n1/s528x528_jfs/t3109/194/2435573156/46587/e0e867ac/57e10978N87220944.jpg!q70.jpg",
        title:"Beats EP头戴式耳机",
        money:558,
        people:1872,
        type:"shu"
      },
      { id:2,
        img:"http://m.360buyimg.com/babel/jfs/t5197/28/400249159/97561/304ce550/58ff0dbeN88884779.jpg!q50.jpg.webp",
        title:"雀巢（Nestle）高钙成人奶粉",
        money:60,
        people:2374,
        type:"shi"
      },
      { id:3,
        img:"http://gw.alicdn.com/tps/TB19OfQRXXXXXbmXXXXL6TaGpXX_760x760q90s150.jpg_.webp",
        title:"煎炒烹炸一锅多用",
        money:216,
        people:351,
        type:"huo"
      },
      { id:4,
        img:"http://gw.alicdn.com/tps/TB1l5psQVXXXXcXaXXXL6TaGpXX_760x760q90s150.jpg_.webp",
        title:"ANNE KLEIN潮流经典没事轻奢",
        money:585,
        people:8191,
        type:"shi"
      },
      { id:5,
        img:"https://m.360buyimg.com/mobilecms/s357x357_jfs/t6490/168/1052550216/653858/9eef28d1/594922a8Nc3afa743.jpg!q50.jpg",
        title:"乐高EV3机器人积木玩具",
        money:3099,
        people:165,
        type:"shu"
      },
      { id:6,
        img:"https://m.360buyimg.com/n1/s220x220_jfs/t1429/17/1007119837/464370/310392f4/55b5e5bfN75daf703.png!q70.jpg",
        title:"LV印花手袋",
        money:1069,
        people:6,
        type:"xie"
      },
      { id:7,
        img:"http://img12.360buyimg.com/n1/s528x528_jfs/t4954/76/635213328/51972/ec4a3c3c/58e5f717N4031d162.jpg!q70.jpg",
        title:"黑色经典版电子书",
        money:805,
        people:395,
        type:"shu"
      },
      { id:8,
        img:"http://m.360buyimg.com/babel/s330x330_jfs/t4597/175/4364374663/125149/4fbbaf21/590d4f5aN0467dc26.jpg!q50.jpg.webp",
        title:"婴儿配方奶粉",
        money:89,
        people:1895,
        type:"mu"
      },
    ],
    showList:[],
    cartList:[],
  },
  mutations: {
    SET_SHOWLIST(state,data){
      console.log(data)
      if(data.type=="all"){
        state.showList=state.phoneList;
      }else if(data.type=="mu"){
        state.showList=state.phoneList.filter(item =>item.type=="mu");
      }else if(data.type=="xie"){
        state.showList=state.phoneList.filter(item =>item.type=="xie");
      }else if(data.type=="shi"){
        state.showList=state.phoneList.filter(item =>item.type=="shi");
        
      }else if(data.type=="shu"){
        state.showList=state.phoneList.filter(item =>item.type=="shu");
      }else if(data.type=="jia"){
        state.showList=state.phoneList.filter(item =>item.type=="jia");
      }
    },
    SET_CARTLIST(state,data){
      const {img,title,money,num,cun,type,select} =data;
      let index=state.cartList.findIndex(item=>item.title==title)
      if(index==-1){
        state.cartList.push({
          img,title,num,money,cun,select,
          tprice:money
        })
      }else{
        if(type=="push"){
          state.cartList[index].num+=1;
            state.cartList[index].tprice+=money;
        }else if(type=="reduce"){
          state.cartList[index].num-=1;
            state.cartList[index].tprice-=money;
            if(!state.cartList[index].num){
              state.cartList.splice(index,1);
            }
        }
      }
      localStorage.setItem('cartList',JSON.stringify(state.cartList));
    },
    GET_CARTLIST(state,data){
      state.cartList=data.cartList;
      
    },
    get_showlist(state,data){
      if(data.type=='all'){
        state.showList.sort((a,b)=>{return a.id - b.id})
      }else if(data.type=='qian'){
        state.showList.sort((a,b)=>{return a.money - b.money})
      }else if(data.type=='buy'){
        state.showList.sort((a,b)=>{return b.people - a.people})
      }
    }
  },
  actions: {
    getCartList({commit}){
      commit('GET_CARTLIST',{
        cartList:JSON.parse(localStorage.getItem('cartList'))||[],
      })
    }
  },
  modules: {
  }
})
